/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **    Update the business service status.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.management.InstanceNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.SessionServerStatus;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.CmdResult;
import com.hp.snap.gr.utils.CmdUtils;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.SSHTools;
import com.hp.snap.gr.utils.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.InstanceNotFoundException;

import java.io.File;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * Update the business service status for running subsystems by JMX API; which support update current site or remote site;
 * 
 * There are two parameter can be configured in the action: Remote and BizServiceStatus.
 * Remote: Is update remote site or not, optional values: true or false.
 * BizServiceStatus: The status to be set; it has two optional values: Standby or Running.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua 
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class SetSiteServiceReadyFlagActionHandler extends AbstractActionHandler {

    // private final static String SNAP_JMX_PREFIX = "ManagementServiceInterface=";
//    private final static String SNAP_JMX_OBJECT = "ATOM:type=Server,name=BizServiceManager";
//    private final static String SNAP_JMX_STATUS_OBJECT = "ATOM:type=Server,name=ATOM Server";
//    private final static String SET_BIZ_STATUS = "setAllBizState";
//    private final static String SS_STATUS = "Status";
    private static Logger logger = LoggerFactory.getLogger(SetSiteServiceReadyFlagActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);
        boolean remote = Boolean.parseBoolean(parameterMap.get("Remote"));
        //String activeFlag = parameterMap.get("BizServiceStatus");
        String runCommandHost = "";
        String runCommandHostIP = "";
        String runCommandSSHUser = "";
        String runSite = "";
//        String result = "";
        String shellscript = parameterMap.get("Shell");
        if (logger.isDebugEnabled()) {
            logger.debug("The shell script is:{}", Utils.maskEIUMPasswd(shellscript));
        }
        
        try {
            CmdResult result = new CmdResult();
            int rs = CmdUtils.runShellScript(shellscript, result, true);
//            actionPlanOutputs.put(Constants.LOCAL_SHELL_STDOUT, result.getStdout().toString());
//            actionPlanOutputs.put(Constants.LOCAL_SHELL_STDERR, result.getStderr().toString());

            if (rs != 0) {
                logger.warn("invoke setting biz server status shell script failed!", runSite);
                return;
            }

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw new ActionException(ex);
        }
        
//        if (remote) {
//            String currentSite = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
//            runSite = Utils.getCorrespondSiteName(currentSite);
//        } else {
//            runSite = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
//        }
//        runCommandHost = Utils.getConfigurationValue(Constants.CFG_GRG_ROOT + "/" + runSite, Constants.CFG_GRG_SITE_GR_HOST);
//        runCommandSSHUser = Utils.getConfigurationValue(Constants.CFG_GRG_ROOT + "/" + runSite, Constants.CFG_GRG_SITE_SSH_USER);
//        runCommandHostIP = Utils.getConfigurationValue(Constants.CFG_GRG_ROOT + "/" + runSite + "/HOST/" + runCommandHost, Constants.CFG_GRG_HOST_IP);
//        if (logger.isDebugEnabled()) {
//            logger.debug("runCommandHost is:{}, runCommandSSHUser is:{}, runCommandHostIP is:{}", new Object[]{runCommandHost, runCommandSSHUser, runCommandHostIP});
//        }
//        SSHTools ssh = new SSHTools(runCommandSSHUser, Constants.SSH_PATH + File.separator + Constants.SSH_KEY_FILE);
//        try {
//            result = ssh.executeRemoteShell(false, shellscript, runCommandHostIP);
//        } catch (Exception e) {
//            logger.warn(e.getMessage());
//            return;
//            //throw new ActionException(e.getMessage(), e.getCause());
//        }
//        int status = ssh.getExitStatusOfCommandResult(result);
//        String resultStr = ssh.getCommandOutput(result);
//        if (status != 0) {
////            logger.error("execute the getting {} session server status shell script failed!", runSiteName);
////            throw new ActionException("execute the getting " + runSiteName + " session server status shell script failed!");
//            logger.warn("invoke setting biz server status shell script failed!", runSite);
//            return;
//        }

    }


}
